package com.share.util.common;

import io.micrometer.common.util.StringUtils;

import java.util.regex.Pattern;

public class PrivacyUtils {
    // 常用正则表达式
    private static final Pattern PHONE_PATTERN = Pattern.compile("^1[3-9]\\d{9}$");
    private static final Pattern EMAIL_PATTERN = Pattern.compile("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6}$");
    private static final Pattern BANK_CARD_PATTERN = Pattern.compile("^[1-9]\\d{14,18}$");
    private static final Pattern ID_CARD_PATTERN = Pattern.compile("^[1-9]\\d{5}(18|19|20)\\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\\d|3[01])\\d{3}[0-9Xx]$");
    private static final Pattern LICENSE_PLATE_PATTERN = Pattern.compile("^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z][A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学警港澳]$");
    private static final Pattern IP_PATTERN = Pattern.compile("^(\\d{1,3}\\.){3}\\d{1,3}$");

    /**
     * 加密姓名（显示姓+最后一个字，中间用*代替）
     */
    public static String encryptName(String name) {
        if (StringUtils.isBlank(name)) return "";

        name = name.trim();
        int len = name.length();
        if (len == 1) return name + "*";
        if (len == 2) return name.charAt(0) + "*";
        return name.charAt(0) + "*" + name.charAt(len - 1);
    }

    /**
     * 严格模式加密姓名（只显示姓）
     */
    public static String strictEncryptName(String name) {
        if (StringUtils.isBlank(name)) return "";
        return name.charAt(0) + "**";
    }

    /**
     * 加密身份证号（显示前3后4位）
     */
    public static String encryptIdCard(String idCard) {
        if (StringUtils.isBlank(idCard)) return "";

        idCard = idCard.trim();
        if (!ID_CARD_PATTERN.matcher(idCard).matches()) {
            return "无效身份证号";
        }
        return idCard.substring(0, 3) + "***********" + idCard.substring(idCard.length() - 4);
    }

    /**
     * 加密手机号（显示前3后4位）
     */
    public static String encryptPhone(String phone) {
        if (StringUtils.isBlank(phone)) return "";

        phone = phone.trim();
        if (!PHONE_PATTERN.matcher(phone).matches()) {
            return "无效手机号";
        }
        return phone.substring(0, 3) + "****" + phone.substring(7);
    }

    /**
     * 加密邮箱（显示首字母和域名）
     */
    public static String encryptEmail(String email) {
        if (StringUtils.isBlank(email)) return "";

        email = email.trim();
        if (!EMAIL_PATTERN.matcher(email).matches()) {
            return "无效邮箱";
        }

        int atIndex = email.indexOf('@');
        if (atIndex <= 1) {
            return email.charAt(0) + "***" + email.substring(atIndex);
        }
        return email.charAt(0) + "***" + email.substring(atIndex - 1);
    }

    /**
     * 加密银行卡号（显示前4后4位）
     */
    public static String encryptBankCard(String bankCard) {
        if (StringUtils.isBlank(bankCard)) return "";

        bankCard = bankCard.trim();
        if (!BANK_CARD_PATTERN.matcher(bankCard).matches()) {
            return "无效银行卡号";
        }

        if (bankCard.length() <= 8) {
            return "****" + bankCard.substring(bankCard.length() - 4);
        }
        return bankCard.substring(0, 4) + "****" + bankCard.substring(bankCard.length() - 4);
    }

    /**
     * 加密地址（显示前几个字符）
     */
    public static String encryptAddress(String address) {
        if (StringUtils.isBlank(address)) return "";

        address = address.trim();
        if (address.length() <= 4) {
            return address.charAt(0) + "****";
        }
        return address.substring(0, 4) + "****";
    }

    /**
     * 加密车牌号（显示前2后2位）
     */
    public static String encryptLicensePlate(String plate) {
        if (StringUtils.isBlank(plate)) return "";

        plate = plate.trim();
        if (!LICENSE_PLATE_PATTERN.matcher(plate).matches()) {
            return "无效车牌号";
        }

        if (plate.length() <= 4) {
            return plate.substring(0, 2) + "**";
        }
        return plate.substring(0, 2) + "***" + plate.substring(plate.length() - 2);
    }

    /**
     * 加密IP地址（IPv4）
     */
    public static String encryptIP(String ip) {
        if (StringUtils.isBlank(ip)) return "";

        ip = ip.trim();
        if (!IP_PATTERN.matcher(ip).matches()) {
            return "无效IP地址";
        }

        String[] segments = ip.split("\\.");
        return segments[0] + "." + segments[1] + ".***.***";
    }

    /**
     * 加密密码（全部用*代替）
     */
    public static String encryptPassword(String password) {
        if (password == null) return "";
        return "******";
    }

    /**
     * 通用加密方法（显示前preLen后sufLen位）
     */
    public static String commonEncrypt(String str, int preLen, int sufLen) {
        if (StringUtils.isBlank(str)) return "";

        str = str.trim();
        if (str.length() <= preLen + sufLen) {
            return "******";
        }

        String prefix = str.substring(0, preLen);
        String suffix = str.substring(str.length() - sufLen);
        return prefix + "***" + suffix;
    }
}